import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { validate } from '@/utils/validateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // ID
  public title = ''; // 分类名称
  public unit = null; // 计价类型
  public weight = ''; // 重量/数量
  public price = ''; // 价格

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  title: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入分类名称',
  },
  unit: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入计价类型',
  },
  weight: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入重量/数量',
  },
  price: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.amount,
  },
};

// 表格搜索表单
export const schemas = []
// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '分类名称',
    key: 'title',
    align: 'left',
    width: -1,
  },
  {
    title: '计价类型',
    key: 'unit',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('category_unit', row.unit);
    },
  },
  {
    title: '重量/数量',
    key: 'weight',
    align: 'left',
    width: -1,
  },
  {
    title: '价格',
    key: 'price',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['category_unit']);
}